package com.iqiyi.pps.epg.core.service.sginal;

import com.iqiyi.pps.epg.api.model.web.query.TFilter;
import com.iqiyi.pps.epg.api.model.web.query.TPage;
import com.iqiyi.pps.epg.api.model.web.sginal.TServerStatus;
import com.iqiyi.pps.epg.api.model.web.sginal.TServerStatusDataPage;
import com.iqiyi.pps.epg.core.dao.base.Page;
import com.iqiyi.pps.epg.core.dao.base.PropertyFilter;
import com.iqiyi.pps.epg.core.dao.sginal.ServerStatusDataDao;
import com.iqiyi.pps.epg.core.model.sginal.ServerStatus;
import com.iqiyi.pps.epg.core.utils.TransUtil;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by zhang on 2017/8/23.
 */
@Service
@Transactional
public class ServerStatusServiceImpl implements ServerStatusService{
    @Autowired
    private ServerStatusDataDao dao;

    @Override
    public ServerStatus get(long id) {
        return dao.get(id);
    }

    @Override
    public void update(ServerStatus entity) {
        dao.save(entity);
    }

    @Override
    public ServerStatus findByServerId(int serverId) {
        Criteria criteria = dao.createCriteria();
        criteria.add(Restrictions.eq("serverId", serverId));
        List<ServerStatus> serverStatuses = dao.find(criteria);
        if (serverStatuses != null && serverStatuses.size() > 0) {
            return serverStatuses.get(0);
        } else {
            return null;
        }

    }

    @Override
    public long add(ServerStatus entity) {
        dao.saveOnly(entity);
        return entity.getId();
    }

    @Override
    public TServerStatusDataPage getServerStatusData(TPage page, TFilter filter) {
        Map<String, String> map = filter.getFilters();
        String tempPattern = "${PARAMS}";
        String totalPattern = "select COUNT(*)";
        List<PropertyFilter> filterList = TransUtil.builderFilterList(filter);
        String hql = tempPattern + " from com.iqiyi.pps.epg.core.model.sginal.ServerStatus";
        Page<ServerStatus> dbPage = TransUtil.buildPage(page);

        hql = TransUtil.buildQueryStringByPropertyFilter(dbPage, hql, filterList);

        if (dbPage.isAutoCount()) {
            String totalCount = dao.getSession().createQuery(hql.replace(tempPattern, totalPattern)).uniqueResult().toString();
            page.setTotalCount(Long.valueOf(totalCount));
        }

        List result = dao.getSession().createQuery(hql.replace(tempPattern, ""))
                .setMaxResults(dbPage.getPageSize())
                .setFirstResult((dbPage.getPageNo() - 1) * dbPage.getPageSize())
                .list();
        TServerStatusDataPage tServerStatusDataPage = new TServerStatusDataPage();
        tServerStatusDataPage.setTPage(page);
        List<TServerStatus> tServerStatusDatas = new ArrayList<TServerStatus>();
        for(Object obj : result) {
            ServerStatus serverStatus = (ServerStatus) obj;
            tServerStatusDatas.add(serverStatus.convertThriftModel());
        }
        tServerStatusDataPage.setResult(tServerStatusDatas);
        return tServerStatusDataPage;
    }
}
